package net.starwifi.duobao.utils;

import com.wo.sdk.message.APIHttpClient;
import com.wo.sdk.message.APIRequest;
import com.wo.sdk.message.AuthorizationHeader;
import com.wo.sdk.openapi.auth.domain.GetTokenResponse;
import com.wo.sdk.openapi.auth.impl.AuthenticateAPIImpl;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by Administrator on 2015/11/26.
 */
public class WoUtils {

    private final static String APP_KEY="218a51a8d5390aa0f36c4958aa12728778a5f33c";

    private final static String APP_SECRET="7784ee3f8da6a01e03bb815699f133dd626bb586";

    /** 获取Token */
    public static String getToken(){
        APIRequest request = new APIRequest();
        AuthorizationHeader header = new AuthorizationHeader();
        header.setAppKey(APP_KEY);
        header.setAppSecret(APP_SECRET);
        request.setAuthHeader(header);
        request.setAccept("application/json");
        request.setContentType("application/json");
        request.setUri("http://open.wo.com.cn/openapi/authenticate/v1.0");
        AuthenticateAPIImpl auth = new AuthenticateAPIImpl();
        GetTokenResponse response = auth.getToken(request);
        if(response.getResultCode()==0){
            return response.getToken();
        }else{
            return null;
        }
    }

    public static String getPhoneNum(String token,String uuid){
        String result = null;
        APIRequest request = new APIRequest();
        AuthorizationHeader header = new AuthorizationHeader();
        header.setAppKey(APP_KEY);
        header.setToken(token);
        request.setAuthHeader(header);
        request.setAccept("application/json");
        request.setContentType("application/json");
        request.setUri("http://open.wo.com.cn/openapi/netplus/getphonenumber/v1.0/" + uuid);
        APIHttpClient client=new APIHttpClient();
        HttpResponse response= null;
        try {
            response = client.doGet(request);
            HttpEntity httpEntity = response.getEntity();
            result = EntityUtils.toString(httpEntity);
            EntityUtils.consume(httpEntity);
            System.out.println("wo----------"+result);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return result;
    }

}
